#ifndef PAGE_H
#define PAGE_H

#include <array>
#include <cstdint>
#include <vector>
#include <string>

// Forward declaration to avoid circular dependency
struct TableInfo;

constexpr int PAGE_SIZE = 4096; // 4KB

// Page header structure
struct PageHeader {
    uint32_t pageId;
    uint32_t nextPageId;
    uint16_t freeSpace;
    uint16_t recordCount;
};

class Page {
public:
    Page(uint32_t pageId);

    uint32_t getPageId() const;
    const std::array<char, PAGE_SIZE>& getData() const;
    std::array<char, PAGE_SIZE>& getData();
    void setDirty(bool dirty);
    bool isDirty() const;

    // Record operation interfaces
    bool insertRecord(const std::vector<std::string>& record, const TableInfo& tableInfo);
    std::vector<std::vector<std::string>> getRecords(const TableInfo& tableInfo) const;
    bool deleteRecord(uint16_t recordId);
    // Add updateRecord method declaration after deleteRecord
    bool updateRecord(uint16_t recordId, const std::vector<std::string>& newValues, const TableInfo& tableInfo);

    // Page header operations
    PageHeader getHeader() const;
    void setHeader(const PageHeader& header);

private:
    uint32_t pageId;
    std::array<char, PAGE_SIZE> data;
    bool dirty;

    // Helper methods
    size_t calculateRecordSize(const std::vector<std::string>& record, const TableInfo& tableInfo) const;
    void serializeRecord(const std::vector<std::string>& record, const TableInfo& tableInfo, char* dest);
    std::vector<std::string> deserializeRecord(const TableInfo& tableInfo, const char* src) const;
};

#endif // PAGE_H